<template>
  <div id="redirect">

  </div>
</template>
<script>
import getUrlParam from '../utils/getUrlParam.js'
import Config from '@/components/project.config.js'
export default {
  mounted(){
    // var state =  getUrlParam.getUrlKey('state')
    // console.log(state)
    // if(state=='redirect'){
    //   console.log(true)
    //   alert('success')
    // }else{
    //   alert('login faluire')http://www.laiyintools.com/#/redirect?code=061B5Upc0XBkIu1lNQpc0BzTpc0B5Upu&state=redirect
    // }
    this.$toast.loading({
      mask: true,
      message: '尝试登陆中...',
      duration: 2000
    });
    console.log('重定向')
    console.log(this.$route)
    this.$store.commit('hideTab')
    console.log(this.$route.query.code)
    console.log(this.$route.query.state)
    if(this.$route.query.state&&this.$route.query.state=='jingmo'){
      console.log('get code')
      this.$http.get(Config.baseUrl+'/api/login/'+this.$route.query.code).then(
        function(rep){
          console.log(rep)
          switch(rep.data.code){
            case 0:
              alert('系统异常')
              break;
            case 1:
            this.$store.commit('setUser',rep.data.data)
            // 登录成功订阅消息接口
              var goEasy = new GoEasy({
                appkey: 'BC-e6ef4c2f42b84bad9f8d3f8ecb100b1a'
              });
              var _this = this
              goEasy.subscribe({
                channel:'channel_'+rep.data.data.id,
                onMessage: function(message){
                  console.log('====================================');
                  console.log(message);
                  console.log('not',_this.$store.state.notification)
                  console.log('mes',_this.$store.state.message)
                  console.log('====================================');
                  _this.$store.state.unread = true
                  var content = JSON.parse(message.content)
                  if(content.type == "RESUME_DELIVER"){
                    _this.$store.state.notification.unshift(content.content)
                  }
                }
              });
              console.log('成功订阅信道For 消息接收 id:' + rep.data.data.id)

              //订阅信道结束
              if(rep.data.data.role==-1){
                this.$router.push({name: 'complete',params:{user: rep.data.data}})

              }else{
                this.$store.commit('setUser',rep.data.data)
                this.$router.push('index')
              }
              break;
            case 2:
            console.log('唤起授权')
              this.$router.push('wxlogin')
              break;
          }
      },function(error){
        // console.log(error)
      })
    }else if(this.$route.query.state&&this.$route.query.state=='redirect'){
      console.log('按钮登录重定向')
      this.$http.get('/api/register/'+this.$route.query.code).then((rsp)=>{
        console.log(rsp)
        if(rsp.status=200&&rsp.data.code==1){
          this.$router.push({name:'complete',params:{user:{id: rsp.data.data}}})
        }
      },(error)=>{
        console.log(error)
      })
    }
  }
}
</script>

<style lang="stylus" scoped>

</style>

